package hibernate;

import com.txm.model.hibernate.ClassRoom;
import com.txm.model.hibernate.Hibernate;
import com.txm.model.hibernate.IDCard;
import com.txm.model.hibernate.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;

import java.util.HashSet;

/**
 * Created by Administrator on 2016/4/11.
 */
public class hibernateTest {

    @Test
    public void test() {
        //创建hibernate配置对象
        Configuration configuration = new Configuration().configure();
        configuration.addAnnotatedClass(Hibernate.class).addAnnotatedClass(IDCard.class).addAnnotatedClass(User.class).addAnnotatedClass(ClassRoom.class);
        //创建服务注册对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        //生成sessionfactory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        User user1 = new User();
        User user2 = new User();
        HashSet<User> users = new HashSet<User>();
        users.add(user1);
        users.add(user2);
        ClassRoom cr = new ClassRoom();
        cr.setUsers(users);
        session.save(user1);
        session.save(user2);
        session.save(cr);
        tx.commit();


    /*    MetadataSources metadataSources = new MetadataSources(serviceRegistry);

        MetadataImplementor metadataImplementor = (MetadataImplementor) metadataSources.buildMetadata();
        SchemaExport export = new SchemaExport();
        EnumSet<TargetType> enumSet = EnumSet.of(TargetType.DATABASE);
        export.create(enumSet, metadataImplementor);*/
    }


}
